<template>
  <div class="container">
    <div class="search-box">
      <el-input placeholder="请输入内容" class="input-box" v-model="findValue"></el-input>
      <el-button @click="removeTasklist">清除</el-button>
      <el-button type="primary" @click="viewTaskList">搜索</el-button>
    </div>
    <el-card style="margin-top: 10px">
      <el-table border style="width: 100%" :data="list">
        <el-table-column type="index" label="任务编号" width="80" :index="indexMethod" />
        <el-table-column prop="name" label="任务名称" />
        <el-table-column prop="des" label="任务描述" />
        <el-table-column fixed="right" label="操作" width="180">
          <template #default="{ row }">
            <el-button size="mini" style="margin-right: 10px" @click.native="editTask(row.id)"
              >编辑</el-button
            >
            <template>
              <el-popconfirm
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="确定删除任务吗？"
                @confirm="delTask(row.id)"
              >
                <el-button size="mini" style="margin-right: 10px" slot="reference">删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <div class="pageContainer">
      <!-- 分页组件 -->
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination
          layout="prev,pager,next"
          :current-page="params._page"
          :total="total"
          :page-size="params._limit"
          @current-change="pageChange"
        />
      </el-row>

      <el-button size="small" plain @click="addfetch">+ 新增</el-button>
    </div>
    <!-- 新增弹框 -->
    <el-dialog title="提示" :visible="dialogVisible" width="30%" @close="closeAssignDialog">
      <!-- 表单内容 -->
      <el-form label-width="100px" ref="taskForm" :model="taskForm" :rules="rules">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="taskForm.name" />
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="taskForm.des" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  addTaskList,
  getTaskDetail,
  updateTaskList,
  deleteTaskList,
  findTaskList
} from '@/api/user.js'
import { v4 as uuidv4 } from 'uuid'
export default {
  name: 'App',
  data() {
    return {
      list: [],
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      id: uuidv4(),
      total: 0,
      dialogVisible: false,
      taskForm: {
        name: '',
        des: '',
        id: ''
      },
      rules: {
        name: [{ required: true, message: '任务名称不能为空', trigger: 'blur' }],
        des: [{ required: true, message: '任务描述不能为空', trigger: 'blur' }]
      },
      findValue: ''
    }
  },
  mounted() {
    this.getTaskList()
  },
  methods: {
    // 序号改为0，2，4，6...
    indexMethod(index) {
      const page = this.params._page
      const pagesize = this.params._limit
      // 当前页数-1*每页的数据条数+index*2+2
      return (page - 1) * pagesize + index * 2 + 2
    },
    async getTaskList() {
      const res = await getList(this.params)
      this.list = res.data
      this.total = +res.headers['x-total-count']
      // console.log(total)
      // console.log(res)
    },
    pageChange(page) {
      this.params._page = page
      this.getTaskList()
    },
    addfetch() {
      this.dialogVisible = true
    },
    closeAssignDialog() {
      this.taskForm = {
        name: '',
        des: '',
        id: ''
      }
      this.$refs.taskForm.resetFields()
      this.dialogVisible = false
    },
    confirmAdd() {
      this.$refs.taskForm.validate(async valid => {
        if (valid) {
          if (this.taskForm.id) {
            await updateTaskList(this.taskForm)
          } else {
            this.taskForm.id = this.id
            await addTaskList(this.taskForm)
          }
          this.id = uuidv4()
          this.getTaskList()
          this.$message.success('操作成功')
          this.dialogVisible = false
          this.taskForm = {
            name: '',
            des: '',
            id: ''
          }
        }
      })
    },
    async editTask(id) {
      this.dialogVisible = true
      const res = await getTaskDetail(id)
      // console.log(res.data)
      this.taskForm = res.data
    },
    async delTask(id) {
      // 调用删除接口
      await deleteTaskList(id)
      // console.log(res.data)
      // 更新列表
      this.getTaskList()
      // // 提示用户
      this.$message.success('删除任务成功')
    },
    async viewTaskList() {
      if (this.findValue) {
        const res = await findTaskList(this.findValue)
        this.list = res.data
        this.$message.success('搜索成功')
      } else {
        this.$message.warning('输入框不能为空')
      }
    },
    removeTasklist() {
      this.findValue = ''
      // this.getTaskList()
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
